package com.oromwin.repos.socket.request.volume;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Common;
import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.ReposWarning;
import com.oromwin.repos.common.Common.ReposResult;
import com.oromwin.repos.common.Common.Status;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.common.helper.DBHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.VolumeTable;
import com.oromwin.repos.server.LockObject;
import com.oromwin.repos.socket.request.AbstractRequest;
import com.oromwin.repos.socket.request.RequestAction;
import com.oromwin.repos.socket.response.Response;
import com.oromwin.repos.socket.response.volume.CreateVolumeResponse;

public class CreateVolumeRequest extends AbstractRequest implements RequestAction {
    private Log log = Logger.getLog(CreateVolumeRequest.class);
    private String VolumeName = null;

    public CreateVolumeRequest() {
    }
    
    public CreateVolumeRequest(String volumeName) {
        VolumeName = volumeName;
    }

    @Override
    public void receive(DataInputStream dis, LockObject lockObject) throws ReposException {
        try {
            VolumeName = IOHelper.readUTF8(dis);
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }

    @Override
    public Response run() {
        CreateVolumeResponse res = null;
        try {
            _volumeTable = new VolumeTable(id(), VolumeName, Status.CREATE, Common.datetime(), Common.datetime());
            checkDependentObjectNotExists();
            DBHelper.insertTable(_volumeTable);
            res = new CreateVolumeResponse(ReposResult.Success, "Succeeded to create volume");
        } catch (ReposWarning e) {
            res = new CreateVolumeResponse(ReposResult.Failure, e.getMessage());
        } catch (ReposException e) {
            res = new CreateVolumeResponse(ReposResult.Failure, e.getMessage());
        }
        return res;
    }

    @Override
    public void send(DataOutputStream dos) throws ReposException {
        try {
            IOHelper.writeUTF8(dos, this.getClass().getCanonicalName());
            IOHelper.writeUTF8(dos, VolumeName);
            dos.flush();
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }

    @Override
    public void checkFreeSpaceExists() throws ReposException {
        throw ReposException.create(log, Common.Message.NOT_IMPLEMENTED_METHOD_INVOKE);
    }

    @Override
    public void checkSameObjectExists() throws ReposException {
        throw ReposException.create(log, Common.Message.NOT_IMPLEMENTED_METHOD_INVOKE);
    }

    @Override
    public void checkDependentObjectNotExists() throws ReposException {
        String whereClause = String.format("VolumeStatus < 2 AND VolumeName = '%s'", VolumeName);
        List<AbstractReposTable> list = DBHelper.selectTable(VolumeTable.class, whereClause);
        if (list.size() != 0) {
            String message = String.format("'%s' VolumeName is already exists at server", VolumeName);
            throw ReposWarning.create(log, message); 
            // throw ReposException.create(log, message);
        }
    }
    
    @Override
    public AbstractReposTable getCurrentReposTable() throws ReposException {
        throw ReposException.create(log, Common.Message.NOT_IMPLEMENTED_METHOD_INVOKE);
    }
    
    @Override
    public AbstractReposTable getParentReposTable() throws ReposException {
        throw ReposException.create(log, Common.Message.NOT_IMPLEMENTED_METHOD_INVOKE);
    }

}
